package com.fivefu.core.hik.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.constant.HikRequestTypeEnum;
import com.fivefu.core.hik.dto.request.VehicleOrgIndexCodes;
import com.fivefu.core.hik.dto.response.EncodeVehicleCameraDetail;
import com.fivefu.core.hik.dto.response.ResponsePage;
import com.fivefu.core.hik.dto.response.ResponseResult;
import com.fivefu.core.hik.entity.DbHikCamera;
import com.fivefu.core.hik.entity.DbHikVehicleCamera;
import com.fivefu.core.hik.services.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HikVehicleCameraServiceImpl implements HikVehicleCameraService {

    @Resource
    private HikvisionService hikvisionService;

    @Resource
    private DbHikVehicleService hikVehicleService;

    @Resource
    private DbHikVehicleCameraService dbHikVehicleCameraService;

    @Resource
    private DbHikCameraService dbHikCameraService;

    /**
     * 根据车辆编号与设备编号获取监控点信息列表V1
     */
    @Override
    public void searchMonitoryPoint() {
        Integer pageNo = 1;
        Integer pageSize = 1000;
        JSONObject exactCondition = null;
        // 获取所有车辆信息的设备与车辆编码列表
        List<Map<String, String>> vehicleInformationMap = hikVehicleService.getVehicleInformationMap();
        // 循环查询监控点与车辆和设备编码的关联关系
        for (Map<String, String> map : vehicleInformationMap) {
            List<String> list = new ArrayList<>();
            exactCondition = new JSONObject();
            // 设备编码
            list.add(map.get("deviceIndexCode"));
            exactCondition.put("deviceIndexCodes", list);
            // 车辆编码
            list = new ArrayList<>();
            list.add(map.get("vehicleIndexCode"));
            exactCondition.put("vehicleIndexCodes", list);
            getMonitoryPoint(pageNo, pageSize, exactCondition);
        }
    }

    /**
     * 根据车辆编号与设备编号获取监控点信息列表V1
     * @param pageNo  当前页码
     * @param pageSize   分页大小
     */
    public void getMonitoryPoint(Integer pageNo, Integer pageSize, Object exactCondition) {
        //根据车辆编号与设备编号获取监控点信息列表V1必需参数
        VehicleOrgIndexCodes vehicleOrgIndexCodes = new VehicleOrgIndexCodes(pageNo, pageSize, exactCondition);
        JSONObject object = JSONObject.parseObject(String.valueOf(vehicleOrgIndexCodes.getExactCondition()));
        String vehicleIndexCode = JSON.parseArray(object.getString("vehicleIndexCodes"), String.class).stream().collect(Collectors.joining(","));
        String deviceIndexCode = JSON.parseArray(object.getString("deviceIndexCodes"), String.class).stream().collect(Collectors.joining(","));
        //将请求对象转换为json字符串
        String body = JSON.toJSONString(vehicleOrgIndexCodes);

        //查询类型枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.VEHICLECAMERADETAIL;

        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        ResponsePage resultPage = new ResponsePage();
        if (null != result && "0".equals(result.getCode())) {
            //调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)) {
                resultPage = JSON.parseObject(resultData, ResponsePage.class);

                resultPage.getList().forEach(deviceObj -> {
                    EncodeVehicleCameraDetail vehicleCameraDetail = JSON.parseObject(JSON.toJSONString(deviceObj), EncodeVehicleCameraDetail.class);
                    DbHikVehicleCamera vehicle = new DbHikVehicleCamera();
                    // 验证数据是否已经存在
                    DbHikVehicleCamera vehicleCamera = dbHikVehicleCameraService.getOne(new QueryWrapper<DbHikVehicleCamera>().eq("is_delete", 0).eq("vehicle_index_code", vehicleIndexCode).eq("device_index_code", deviceIndexCode).eq("channel_num",  vehicleCameraDetail.getChannelNum()));
                    if (vehicleCamera == null) {
                        // 车辆监控点关联表赋值
                        vehicle = vehicleCameraDetail.replaceDbHikVehicle(vehicleIndexCode);
                    } else {
                        // 车辆监控点关联表赋值
                        vehicle = vehicleCameraDetail.replaceDbHikVehicleModify(vehicleIndexCode, vehicleCamera.getId());
                    }
                    //编辑海康车辆监控点关联信息
                    dbHikVehicleCameraService.editHikCamera(vehicle);
                    // 监控点赋值
                    DbHikCamera camera = vehicleCameraDetail.replaceDbHikCameraModify();
                    // 编辑海康监控点信息
                    dbHikCameraService.editHikCamera(camera);
                });

                //循环查询分页数据
                if (resultPage.getTotal() > ((pageNo - 1) * pageSize + pageSize)){
                    //调用方法本身，递归完成查询
                    getMonitoryPoint(pageNo + 1,pageSize, vehicleOrgIndexCodes.getExactCondition());
                }
            }
        }
    }
}
